package lyl.queue;

import java.util.Stack;

/**
 * stack1存
 * pop/peek时从stack2取
 * 如果stack2没数据就从stack1里面加载
 * stack1里面的都是后进入的
 */
public class ImplementQueueByStack {

    Stack<Integer> stack1;
    Stack<Integer> stack2;

    ImplementQueueByStack(){
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void add(int element){
        stack1.push(element);
    }

    public Integer poll(){
        if(stack1.isEmpty() && stack2.isEmpty()){
            return null;
        }
        if(stack2.isEmpty()){
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
        return stack2.pop();
    }

    public Integer peek(){
        if(stack1.isEmpty() && stack2.isEmpty()){
            return null;
        }
        if(stack2.isEmpty()){
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
        return stack2.peek();
    }
}
